/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_cicsts_report_msg_data_req.c                     */
/*  NOTE      =                                                    */
/*  DATE      = 2016/05/03 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "frct_comm.h"

#include <cicsts/config.h>
#include <cicsts/cicsts_core.h>
#include <cicsts/cicsts_comm.h>
#include <cicsts/cicsts_packet.h>
#include <apmtest_comm.h>
#include <frreport_opt.h>
#include "../test_cicsts_comm.h"

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

static void __case1(void)
{
    Conntrack *p_st_conn = NULL;
    Frreport_body_data *p_st_body = NULL;
    L7_cicsts_stmt *p_st_stmt = NULL;
    Frlist_node *p_st_node = NULL;

    CU_ASSERT_EQUAL(test_cicsts_create_ct(&p_st_conn, TEST_CICSTS_SRC_IP, TEST_CICSTS_DST_IP, TEST_CICSTS_SRC_PORT, TEST_CICSTS_DST_PORT), FR_SUCC);

    L7_session *p_st_session = frct_get_session(p_st_conn);

    L7session_cicsts_private *p_st_session_private = (L7session_cicsts_private *) frct_get_session_private(p_st_session);
    L7_cicsts_packet *p_st_cicsts_packet = p_st_session_private->p_st_cicsts_packet;

    char *p_bin = (char *) "000002360000107b000b90a0270502ff0003d1004004c3d7d4c9180b02c3c9c3e2e4e2c5d940400b01bf000000000000000000020312f20a8502020102000001040e430e02000007e0000000800100000a02a2968194818995000504200001d8063c3f786d6c2076657273696f6e3d22312e30223f3e3c54726164653e3c526571756573744865616465723e3c627573696e65737341636365707449643e373630323031363034323033333835323830333c2f627573696e65737341636365707449643e3c73657269616c4e756d6265723e303c2f73657269616c4e756d6265723e3c76657273696f6e3e312e303c2f76657273696f6e3e3c636d64547970653e303c2f636d64547970653e3c7265717565737465723e545359573c2f7265717565737465723e3c6368616e6e656c3e435a42303c2f6368616e6e656c3e3c7265717565737454696d655374616d703e323031362d30342d32302031313a33343a33312e3030303330313c2f7265717565737454696d655374616d703e3c6f7267616e697a6174696f6e556e697449642f3e3c74656c6c6572313e747379773c2f74656c6c6572313e3c74656c6c6572323e747379773c2f74656c6c6572323e3c7365727669636549643e68715a4858583c2f7365727669636549643e3c2f526571756573744865616465723e3c426f64793e3c7a686c786d2f3e3c7a683e333031303030303031303132303130303032383332383c2f7a683e3c7a6878682f3e3c6a676d2f3e3c2f426f64793e3c2f54726164653e";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_CICSTS_SRC_IP, TEST_CICSTS_DST_IP, TEST_CICSTS_SRC_PORT, TEST_CICSTS_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_SRV2CLIENT;

    CU_ASSERT_EQUAL(cicsts_packet_unfragment(p_st_packet, p_st_cicsts_packet), APM_CICSTS_CHECK_FRAGMENT_SUCCESS);

    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    p_st_stmt = (L7_cicsts_stmt *) frmalloc(sizeof(L7_cicsts_stmt));
    CU_ASSERT_PTR_NOT_NULL(p_st_stmt);
    memset(p_st_stmt, 0x00, sizeof(L7_cicsts_stmt));
    p_st_stmt->ui_msg_id = p_st_cicsts_packet->ui_msg_id;

    p_st_node = frlist_node_create(p_st_stmt);
    CU_ASSERT_EQUAL(frhash2_addLong(p_st_session_private->p_hash_stmt, p_st_cicsts_packet->ui_msg_id, p_st_node), FR_SUCC);

    p_st_body = frreport_body_new_l7(p_st_conn);
    CU_ASSERT_PTR_NOT_NULL(p_st_body);

    CU_ASSERT_EQUAL(frreport_body_l7_add_str(p_st_body, APM_REPORT_KEY_TRANS_TITLE, "test"), FR_SUCC);

    CU_ASSERT_EQUAL(cicsts_report_msg_data_req(p_st_conn, p_st_cicsts_packet, p_st_body, &st_hash_report, p_st_stmt), FR_SUCC);

    Frvar_field *p_st_field = NULL;
    Frlist_header *p_list_report = NULL;
    Frlist_node *p_st_node_report = NULL;
    Frreport_data *p_st_report_data = NULL;
    p_list_report = &((&st_hash_report)->p_list_array[0]);

    p_st_node_report = FRLIST_FIRST_NODE(p_list_report);
    CU_ASSERT_PTR_NOT_NULL(p_st_node_report);

    p_st_report_data = (Frreport_data *) FRLIST_GET_NODE_DATA(p_st_node_report);
    CU_ASSERT_PTR_NOT_NULL(p_st_report_data);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_TRANS_TITLE, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->p_field_name, "transaction.title.s");
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "test");

    frhash2_free(&st_hash_report, list_report_node_free);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    test_cicsts_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

int add_suite_cicsts_report_msg_data_req(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" cicsts_report_msg_data_req()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
